package com.songlin.testv2ex.android_gson_json;

import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 松林 on 2015/10/5.
 */
public class GsonTools {

    public GsonTools() {

    }

    public static <T> T getTopic(String jsonString, Class<T> cls) {
        T t = null;
        try {
            Gson gson = new Gson();
            t = gson.fromJson(jsonString,cls);
        } catch(Exception ex) {
            Log.d("Throws Exception", "From \"public static<T> T getPerson(String jsonString, Class<T> cls) \" in GsonTools.java");
            ex.printStackTrace();
        }
        return t;
    }

    public static <T> List<T> getTopics(String jsonString,Class<T> cls) {
        List<T> list = new ArrayList<T>();
        try {
            Gson gson = new Gson();
            list = gson.fromJson(jsonString, new TypeToken<List<T>>() {
            }.getType());
        } catch(Exception ex) {
            Log.d("Throws Exception", "From \"public static <T> List<T> getPersons(String jsonString,Class<T> cls) \" in GsonTools.java");
            ex.printStackTrace();
        }

        return list;
    }
}